---@class BP_Player_C:Character
---@field STCharacterMovement_0 USTCharacterMovementComponent
---@field SkeletalMesh USkeletalMeshComponent
---@field ID int32
---@field TeamIndex int32
--Edit Below--
require("Script.Common.Debug")

local BP_Player = {
    CurrentTileIndex = 0;
    isValid = true;
};
PlayerTest = nil

--DS
--local isValid = true
--local playernum = UGCGameSystem.GameState.PlayerNum
function BP_Player:ReceiveTick(DeltaTime)
    print("ReceiveTick whoa"..self.TeamIndex)
    if self:HasAuthority() then
        if PlayerTest == nil then
            PlayerTest = self
        end
        print("ReceiveTick whob"..self.TeamIndex)
        if (self.isValid) then
            if #UGCGameSystem.GameState.TileList ~= 0 then
                --随机化得出初始位置
                local CanInitTile = {}
                print(string.format("tilelistgnkagk[%d]", #UGCGameSystem.GameState.TileList)) --279
                print("ReceiveTick whoc"..self.TeamIndex)
                for i = 1, #UGCGameSystem.GameState.TileList do
                    if UGCGameSystem.GameState.TileList[i].Level == 1 then
                        table.insert(CanInitTile, UGCGameSystem.GameState.TileList[i])
                    end
                end
                local InitTile = CanInitTile[math.random(1, #CanInitTile)].ID -- TODO:random有报错
                self.CurrentTileIndex = InitTile
                Debug:Log("P_Player:ReceiveTick", self.CurrentTileIndex)
                --设置玩家的初始位置
                local pos = UGCGameSystem.GameState.TileList[InitTile].Pos:K2_GetComponentLocation()
                pos = { X = pos.X, Y = pos.Y, Z = pos.Z + 100 }
                print("qqqx:" .. pos.X .. " y:" .. pos.Y .. " z:" .. pos.Z)
                self:K2_SetActorLocation(pos)
                self.isValid = false
                --[[
                playernum = playernum - 1
                if(playernum == 0) then
                    isValid = false
                end
                ]]
            end
        end
    end
end


--执行顺序的问题
function BP_Player:ReceiveBeginPlay()
    print("bp_player receivebeginplay")
    self.SuperClass.ReceiveBeginPlay(self);

    print("BP_Player:ReceiveBeginPlay");
end

function BP_Player:GetReplicatedProperties()
    return "CurrentTileIndex"
    --"CurrentState"
end

-- 获取当前脚下的地砖对象
function BP_Player:GetCurTile()
    print("Chess:GetCurTile CurrentTile is:" .. tostring(self.CurrentTileIndex));
    return UGCGameSystem.GameState.TileList[self.CurrentTileIndex]
end

--[[
function BP_Player:PawnMoveToLocation(position)
    ugcprint("Chess:PawnMoveToLocation")
    local controller = self:GetController()
    if controller == nil then
        print("Chess:PawnMoveToLocation controller is nil")
        return
    end
    controller:MoveToLocation((VectorHelper.ToLuaTable(position)))
end
]] --

--[[
function BP_Player:SetAvatar(PlayerAvatarData)

    if self.CharacterAvatarComp_BP then
        self.CharacterAvatarComp_BP.forceClientMode = false;

        for _, AvatarItemID in ipairs(PlayerAvatarData.AvatarItemIDList) do
            local SlotID = BackpackUtils.GetEquipSlotID(AvatarItemID)

            --Slot1类型为初始化模型Avatar
            if SlotID == 1 then
                self.CharacterAvatarComp_BP:InitDefaultAvatarByResID(PlayerAvatarData.Gender, AvatarItemID, 0)
            else
                self.CharacterAvatarComp_BP:PutOnEquipmentByResID(AvatarItemID)
            end
        end
    end
end
]] --

--[[
function BP_Player:SetActorLocation(loc)
	print("BP_Player:SetActorLocation")
    local targetLoc = {X = loc.X, Y = loc.Y, Z = self:K2_GetActorLocation().Z}
    self:K2_SetActorLocation(targetLoc);
    self:GetController():StopMovement()
end
]] --

--[[
function BP_Player:CheckChessComplete()
    print("Chess:CheckChessComplete currentState:" .. tostring(self.CurrentState));
    if UGCGameSystem.GameState == nil then
        print("Chess:CheckChessComplete Gamestate is nil");
        return;
    end
    if self.CurrentState == 4 then
        if not self.bHasShowCrown then
            self:SetCrownVisible(true)
            local SpawnEmitterLocation = VectorHelper.Add(VectorHelper.ToLuaTable(self:K2_GetActorLocation()), {X = 0, Y = 0, Z = 200});
            -- 异步加载特效
            UGCParticleTools:AsyncLoadWithCallback(UGCParticleTools.List.CrownLight,        
            function (Particle)
                STExtraBlueprintFunctionLibrary.SpawnCustomEmitterAtLocation(self,Particle, SpawnEmitterLocation, {Roll=0, Pitch=0, Yaw=0}, false)
            end)
            self.bHasShowCrown = true;
        else
            print("Chess:CheckChessComplete bHasShowCrown is true");
        end
    end
end
]] --

--[[
function BP_Player:OnRep_CurrentState()
    print("Chess:OnRep_CurrentState currentState:" .. tostring(self.CurrentState));
    self:CheckChessComplete();
end
]] --


function BP_Player:OnRep_CurrentTileIndex()
    print("Chess:OnRep_CurrentTileIndex CurrentTileIndex:" .. tostring(self.CurrentTileIndex));
end

return BP_Player;
